<?php
 

define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hd_settings.inc.php');
require_once('language/'.$hd_settings['language'].'.inc.php');
require_once('inc/common.inc.php');

/* Connect to database */
require_once('inc/database.inc.php');
hd_dbConnect();

hd_session_start();
hd_isLoggedIn();
/* Must be administrator to access this page */
hd_isAdmin();

$set=array();

$set['site_title']=hd_input($_POST['s_site_title'],$hdlang['err_sname']);
$set['site_title']=str_replace('\\&quot;','&quot;',$set['site_title']);
$set['site_url']=hd_validateURL($_POST['s_site_url'],$hdlang['err_surl']);

$set['support_mail']=hd_validateEmail($_POST['s_support_mail'],$hdlang['err_supmail']);
$set['webmaster_mail']=hd_validateEmail($_POST['s_webmaster_mail'],$hdlang['err_wmmail']);
$set['noreply_mail']=hd_validateEmail($_POST['s_noreply_mail'],$hdlang['err_nomail']);

$set['hd_title']=hd_input($_POST['s_hd_title'],$hdlang['err_htitle']);
$set['hd_title']=str_replace('\\&quot;','&quot;',$set['hd_title']);
$set['hd_url']=hd_validateURL($_POST['s_hd_url'],$hdlang['err_hurl']);
$set['server_path']=hd_input($_POST['s_server_path'],$hdlang['err_spath']);
 $set['language']=hd_input($_POST['s_language'],$hdlang['err_lang']);
$set['language']=str_replace('.inc.php','',$set['language']);
if (!file_exists('language/'.$set['language'].'.inc.php')) {hd_error($hdlang['err_nolang']);}
$set['max_listings']=hd_input($_POST['s_max_listings'],$hdlang['err_max']);
$set['print_font_size']=hd_input($_POST['s_print_font_size'],$hdlang['err_psize']);
$set['debug_mode']=$_POST['s_debug_mode'] ? 1 : 0;
$set['secimg_use']=$_POST['s_secimg_use'] ? 1 : 0;
$set['secimg_sum']='';
for ($i=1;$i<=10;$i++) {
    $set['secimg_sum'] .= substr('AEUYBDGHJLMNPQRSTVWXZ123456789', rand(0,29), 1);
}

$set['database_host']=hd_input($_POST['s_database_host'],$hdlang['err_dbhost']);
$set['database_name']=hd_input($_POST['s_database_name'],$hdlang['err_dbname']);
$set['database_user']=hd_input($_POST['s_database_user'],$hdlang['err_dbuser']);
$set['database_pass']=hd_input($_POST['s_database_pass'],$hdlang['err_dbpass']);
$set_link = @mysql_connect($set['database_host'],$set['database_user'],$set['database_pass']) or hd_error($hdlang['err_dbconn']);
if (!(@mysql_select_db($set['database_name'],$set_link))) {hd_error($hdlang['err_dbsele']);}
mysql_close($set_link);

$set['attachments']['use']=$_POST['s_attach_use'] ? 1 : 0;
if ($set['attachments']['use']) {
    $set['attachments']['max_number']=hd_isNumber($_POST['s_max_number']) ? $_POST['s_max_number'] : 2;
    $set['attachments']['max_size']=hd_isNumber($_POST['s_max_size']) ? $_POST['s_max_size'] : 512;
    $set['attachments']['allowed_types']=hd_input($_POST['s_allowed_types']);
    if (empty($set['attachments']['allowed_types'])) {
        $set['attachments']['allowed_types']=array('.gif','.jpg','.jpeg','.zip','.rar','.csv','.doc','.txt','.pdf');
    } else {
        $set['attachments']['allowed_types']=explode(',',str_replace(' ','',$set['attachments']['allowed_types']));
    }
} else {
    $set['attachments']['max_number']=2;
    $set['attachments']['max_size']=512;
    $set['attachments']['allowed_types']=array('.gif','.jpg','.jpeg','.zip','.rar','.csv','.doc','.txt','.pdf');
}

$set['use_custom']=$_POST['s_use_custom'] ? 1 : 0;
$set['custom_place']=$_POST['s_custom_place'] ? 1 : 0;
if ($set['use_custom']) {

    for ($i=1;$i<=5;$i++) {
        $this_field='custom' . $i;

        $set['custom_fields'][$this_field]['use']=$_POST['s_custom'.$i.'_use'] ? 1 : 0;
        if ($set['custom_fields'][$this_field]['use']) {
            $set['custom_fields'][$this_field]['req']=$_POST['s_custom'.$i.'_req'] ? 1 : 0;
            $set['custom_fields'][$this_field]['name']=hd_input($_POST['s_custom'.$i.'_name'],$hdlang['err_custname']);
            $set['custom_fields'][$this_field]['maxlen']=hd_isNumber($_POST['s_custom'.$i.'_maxlen']) ? $_POST['s_custom'.$i.'_maxlen'] : 255;
        } else {
            $set['custom_fields'][$this_field] = array('use'=>0,'req'=>0,'name'=>'Custom field '.$i,'maxlen'=>255);
        }
    }

} else {
    $set['custom_fields']=array (
        'custom1'  => array('use'=>0,'req'=>0,'name'=>'Custom field 1','maxlen'=>255),
        'custom2'  => array('use'=>0,'req'=>0,'name'=>'Custom field 2','maxlen'=>255),
        'custom3'  => array('use'=>0,'req'=>0,'name'=>'Custom field 3','maxlen'=>255),
        'custom4'  => array('use'=>0,'req'=>0,'name'=>'Custom field 4','maxlen'=>255),
        'custom5'  => array('use'=>0,'req'=>0,'name'=>'Custom field 5','maxlen'=>255)
    );
}

$settings_file_content='<?php
/* Settings file for hd ' . $hd_settings['hd_version'] . ' */
/*** Please read the README.HTM file for more information on these settings ***/

/* Website settings */
$hd_settings[\'site_title\']=\'' . $set['site_title'] . '\';
$hd_settings[\'site_url\']=\'' . $set['site_url'] . '\';

/* Contacts */
$hd_settings[\'support_mail\']=\'' . $set['support_mail'] . '\';
$hd_settings[\'webmaster_mail\']=\'' . $set['webmaster_mail'] . '\';
$hd_settings[\'noreply_mail\']=\'' . $set['noreply_mail'] . '\';

/* Help desk settings */
$hd_settings[\'hd_url\']=\'' . $set['hd_url'] . '\';
$hd_settings[\'hd_title\']=\'' . $set['hd_title'] . '\';
$hd_settings[\'server_path\']=\'' . $set['server_path'] . '\';
$hd_settings[\'language\']=\'' . $set['language'] . '\';
$hd_settings[\'max_listings\']=' . $set['max_listings'] . ';
$hd_settings[\'print_font_size\']=' . $set['print_font_size'] . ';
$hd_settings[\'debug_mode\']=' . $set['debug_mode'] . ';
$hd_settings[\'secimg_use\']=' . $set['secimg_use'] . ';
$hd_settings[\'secimg_sum\']=\'' . $set['secimg_sum'] . '\';

/* Database settings */
$hd_settings[\'database_host\']=\'' . $set['database_host'] . '\';
$hd_settings[\'database_name\']=\'' . $set['database_name'] . '\';
$hd_settings[\'database_user\']=\'' . $set['database_user'] . '\';
$hd_settings[\'database_pass\']=\'' . $set['database_pass'] . '\';

/* File attachments */
$hd_settings[\'attachments\']=array (
    \'use\' =>  ' . $set['attachments']['use'] . ',
    \'max_number\'  =>  ' . $set['attachments']['max_number'] . ',
    \'max_size\'    =>  ' . $set['attachments']['max_size'] . ', // kb
    \'allowed_types\'   =>  array(\'' . implode('\',\'',$set['attachments']['allowed_types']) . '\')
);

/* Custom fields */
$hd_settings[\'use_custom\']=' . $set['use_custom'] . ';
$hd_settings[\'custom_place\']=' . $set['custom_place'] . ';
$hd_settings[\'custom_fields\']=array (
';

for ($i=1;$i<=5;$i++) {
    $settings_file_content.='\'custom'.$i.'\'=>array(\'use\'=>'.$set['custom_fields']['custom'.$i]['use'].',\'req\'=>'.$set['custom_fields']['custom'.$i]['req'].',\'name\'=>\''.$set['custom_fields']['custom'.$i]['name'].'\',\'maxlen\'=>'.$set['custom_fields']['custom'.$i]['maxlen'].')';
    if ($i!=5) {$settings_file_content.=',
';}
}

$settings_file_content.='
);

#############################
#     DO NOT EDIT BELOW     #
#############################
$hd_settings[\'hd_version\']=\'' . $hd_settings['hd_version'] . '\';
if ($hd_settings[\'debug_mode\']) {
    error_reporting(E_ALL ^ E_NOTICE);
} else {
    ini_set(\'display_errors\', 0);
    ini_set(\'log_errors\', 1);
}
if (!defined(\'IN_SCRIPT\')) {die(\'Invalid attempt!\');}
if (is_dir(\'install\') && !defined(\'INSTALL\')) {die(\'Please delete the <b>install</b> folder from your server for security reasons then refresh this page!\');}
?>';

/*echo $settings_file_content;
exit();*/

$fp=@fopen('hd_settings.inc.php','w') or hd_error($hdlang['err_openset']);
fputs($fp,$settings_file_content);
fclose($fp);

/* Print header */
require_once('inc/header.inc.php');

/* Print main manage users page */
require_once('inc/show_admin_nav.inc.php');
?>

</td>
</tr>
<tr>
<td>

<p>&nbsp;</p>
<h3 align="center"><?php echo $hdlang['set_saved']; ?></h3>

<p>&nbsp;</p>

<p align="center"><?php echo $hdlang['set_were_saved']; ?></p>

<p align="center"><a href="admin_settings.php"><?php echo $hdlang['settings']; ?></a> |
<a href="admin_main.php"><?php echo $hdlang['main_page']; ?></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<!-- HR -->
<p>&nbsp;</p>

<?php
require_once('inc/footer.inc.php');
exit();
?>
